package com.shiro.dao;

import com.okok.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;



@Repository("userSimpleDao")
public class UserSimpleDaoImpl implements UserSimpleDao {

	private static Logger logger = LoggerFactory
			.getLogger(UserSimpleDaoImpl.class);

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public User findByLoginName(String loginName, String app) {

		Criteria criteria = Criteria.where("delflag").is(false);
		criteria.and("loginName").is(loginName);
		
		if (StringUtils.isNotBlank(app)) {
			criteria.and("app").is(app);
		}
		
		Query query = new Query(criteria);
		User user = mongoTemplate.findOne(query, User.class);
		if (null == user) {
			logger.info("findByLoginName not find user");
		} else {
			logger.info("findByLoginName user: " + user.getLoginName());
		}

		return user;
	}


	
}